package cn.com.bluemoon.daps.standard.controller;


import cn.com.bluemoon.daps.common.datascope.BmPermission;
import cn.com.bluemoon.daps.common.domain.ResultBean;
import cn.com.bluemoon.daps.standard.entity.DapStandardDim;
import cn.com.bluemoon.daps.standard.service.DapStandardDimService;
import cn.com.bluemoon.daps.standard.vo.DimSearchVo;
import cn.com.bluemoon.daps.standard.vo.StandardDimVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据标准维度 前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-09-23
 */
@RestController
@Slf4j
@RequestMapping("/stdDim")
@Api(tags = {"维度管理"})
public class DapStandardDimController {

    @Resource
    DapStandardDimService dimService;

    @GetMapping
    @ApiOperation(value = "维度列表")
    @BmPermission(value = "standard:dim:list", name = "维度列表")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = StandardDimVo.class, responseContainer = "List")})
    public ResultBean<List<StandardDimVo>> dims() {
        List<DapStandardDim> res = dimService.list(dimService.bmLambdaQuery());
        return ResultBean.ok(res)
                .convert(dims -> dims.stream().map(StandardDimVo::new).collect(Collectors.toList()));
    }

    @PostMapping("page/list")
    @ApiOperation(value = "维度列表,分页")
    @BmPermission(value = "standard:dim:list", name = "维度列表")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapStandardDim.class, responseContainer = "List")})
    public IPage<DapStandardDim> dimList(@RequestBody @Valid DimSearchVo vo) {

        return dimService.getDimList(vo);
    }

    @PostMapping("insert")
    @ApiOperation(value = "新增维度")
    @BmPermission(value = "standard:dim:add", name = "新建维度")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<String> addStandardDims(@RequestBody @Valid DapStandardDim dapStandardDim) {

        return dimService.addDapStandardDim(dapStandardDim);
    }

    @GetMapping("info")
    @ApiOperation(value = "查看维度")
    @BmPermission(value = "standard:dim:info", name = "查看维度")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = DapStandardDim.class)})
    public ResultBean<DapStandardDim> standardDimInfo(@RequestParam("id") @ApiParam("维度id") String id) {

        return dimService.dapStandardDimInfo(id);
    }

    @PostMapping("update")
    @ApiOperation(value = "修改维度")
    @BmPermission(value = "standard:dim:update", name = "修改维度")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<String> updateStandardDims(@RequestBody @Valid DapStandardDim dapStandardDim) {

        return dimService.updateDapStandardDim(dapStandardDim);

    }

    @PostMapping("delete")
    @ApiOperation(value = "删除维度")
    @BmPermission(value = "standard:dim:delete", name = "删除维度")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "ok", response = ResultBean.class)})
    public ResultBean<String> deleteStandardDims(@RequestBody List<String> dimIds) {

        return dimService.deleteDapStandardDim(dimIds);

    }
}
